<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/3/7
 * Time: 20:06
 */

namespace app\admin\controller;

use app\admin\logic\AdminLogic;
use app\admin\logic\RoleLogic;
use app\common\model\mysql\AuthGroupAccess;
use think\App;
use think\Request;

class Admin extends Base
{
    protected $logic = null;
    protected $roleLogic = null;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->logic     = new AdminLogic();
        $this->roleLogic = new RoleLogic();
    }

    public function profile()
    {
        return view();
    }

    /**
     * @return \think\response\View
     */
    public function index()
    {
        $where          = [];
        $where['field'] = [
            'id',
            'username',
            'nickname',
            'email',
            'mobile',
            'last_login_ip',
            'last_login_time',
            'create_time',
            'status',
            'is_admin',
        ];
        $where['order'] = "id desc";
        $lists          = $this->logic->getLists($where);

        return view('admin/index', compact('lists'));
    }

    private function _postData(Request $request)
    {
        $data = [
            'username' => $request->post('username', '', 'trim'),
            'nickname' => $request->post('nickname', '', 'trim'),
            'email'    => $request->post('email', '', 'trim'),
            'mobile'   => $request->post('mobile', '', 'trim'),
            'status'   => $request->post('status', 0, 'intval'),
            'password' => $request->post('password', '', 'trim'),
        ];

        return $data;
    }

    /**
     * @param Request $request
     * @return \think\response\View
     */
    public function add(Request $request)
    {
        if ($request->isPost()) {
            $data     = $this->_postData($request);
            $group_id = $request->post('group_id', 0, 'intval');
            try {
                $res = $this->logic->saveData($group_id, $data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            if ($res) {
                $this->success($data['username'].'管理员添加成功', url('Admin/index'));
            }
            $this->error('添加失败');

        } else {
            // 获取所有角色, id, title
            $where           = [];
            $where['status'] = 1;
            $where['field']  = "id,title";
            $roles           = $this->roleLogic->getLists($where);

            return view('admin/add', compact('roles'));
        }
    }

    public function edit(Request $request)
    {
        if ($request->isPost()) {
            $id         = $request->post('id', 0, 'intval');
            $group_id   = $request->post('group_id', 0, 'intval');
            $updateData = $this->_postData($request);
            try {
                $res = $this->logic->updateAdminAndGroupAccessById($id, $updateData, $group_id);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if ($res) {
                $this->success($updateData['username'].'编辑成功', url('Admin/index'));
            }
            $this->error('编辑失败');
        } else {
            $id = $request->param('id', 0, 'intval');
            // 根据管理员id获取角色组id
            $group_id = AuthGroupAccess::where('uid', $id)->value('group_id');
            // 获取管理员基础信息
            $admin_one = $this->logic->findOne($id);

            // 获取所有角色, id, title
            $where           = [];
            $where['status'] = 1;
            $where['field']  = "id,title";
            $roles           = $this->roleLogic->getLists($where);

            return view('admin/edit', compact('group_id', 'admin_one', 'roles'));
        }
    }

    public function delete()
    {
        $id = $this->request->post('id', 0, 'intval');
        try {
            $res = $this->logic->deleteById($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if ($res) {
            $this->success("删除成功", url('Role/index'));
        }
        $this->error('删除失败');
    }
}